import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter/foundation.dart'; // 添加这个导入以使用kIsWeb
import 'package:flutter/gestures.dart'; // 添加这个导入以使用PointerDeviceKind

/// 下拉刷新和上拉加载更多的统一封装组件
/// 提供了统一的刷新和加载状态管理
class PullRefreshWrapper extends StatelessWidget {
  /// 子组件
  final Widget child;

  /// 下拉刷新回调函数
  final Future<void> Function()? onRefresh;

  /// 上拉加载更多回调函数
  final Future<void> Function()? onLoadMore;

  /// 是否启用下拉刷新
  final bool enableRefresh;

  /// 是否启用上拉加载
  final bool enableLoadMore;

  /// 控制器
  final RefreshController? controller;

  /// 下拉刷新的头部样式
  final Widget? header;

  /// 上拉加载的底部样式
  final Widget? footer;

  /// 列表为空时显示的组件
  final Widget? emptyWidget;

  /// 是否为空列表
  final bool isEmpty;

  const PullRefreshWrapper({
    Key? key,
    required this.child,
    this.onRefresh,
    this.onLoadMore,
    this.enableRefresh = true,
    this.enableLoadMore = true,
    this.controller,
    this.header,
    this.footer,
    this.emptyWidget,
    this.isEmpty = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 使用外部传入的控制器或创建一个新的
    final RefreshController _controller = controller ?? RefreshController();

    // 处理下拉刷新
    Future<void> _handleRefresh() async {
      if (onRefresh != null) {
        try {
          await onRefresh!();
        } catch (e) {
          print('Refresh error: $e');
        } finally {
          // 确保刷新结束后调用完成
          _controller.refreshCompleted();
        }
      } else {
        _controller.refreshCompleted();
      }
    }

    // 处理上拉加载更多
    Future<void> _handleLoadMore() async {
      if (onLoadMore != null) {
        try {
          await onLoadMore!();
        } catch (e) {
          print('Load more error: $e');
          _controller.loadFailed();
        } finally {
          // 加载完成后调用加载完成
          _controller.loadComplete();
        }
      } else {
        _controller.loadComplete();
      }
    }

    // 如果列表为空，显示空状态组件
    if (isEmpty && emptyWidget != null) {
      return emptyWidget!;
    }

    // 使用ScrollConfiguration来增强Web端的滚动行为
    final smartRefresher = SmartRefresher(
      controller: _controller,
      enablePullDown: enableRefresh && onRefresh != null,
      enablePullUp: enableLoadMore && onLoadMore != null,
      // 针对Web端添加配置
      physics: kIsWeb ? const ClampingScrollPhysics() : null,
      header: header ??
          const ClassicHeader(
            refreshingIcon: SizedBox(
              width: 20.0,
              height: 20.0,
              child: CircularProgressIndicator(strokeWidth: 2.0),
            ),
            releaseText: '释放刷新',
            refreshingText: '刷新中...',
            completeText: '刷新完成',
            idleText: '下拉刷新',
            textStyle: TextStyle(fontSize: 12.0),
          ),
      footer: footer ??
          const ClassicFooter(
            loadingIcon: SizedBox(
              width: 20.0,
              height: 20.0,
              child: CircularProgressIndicator(strokeWidth: 2.0),
            ),
            loadingText: '加载中...',
            noDataText: '没有更多数据了',
            idleText: '上拉加载更多',
            failedText: '加载失败',
            textStyle: TextStyle(fontSize: 12.0),
          ),
      onRefresh: _handleRefresh,
      onLoading: _handleLoadMore,
      child: child,
    );

    // Web端特殊处理：使用ScrollConfiguration来确保鼠标滚动事件正确传递
    if (kIsWeb) {
      return ScrollConfiguration(
        behavior: ScrollConfiguration.of(context).copyWith(
          dragDevices: {
            PointerDeviceKind.touch,
            PointerDeviceKind.mouse, // 添加鼠标支持
          },
          scrollbars: true,
        ),
        child: smartRefresher,
      );
    }

    return smartRefresher;
  }
}